/*幻灯片轮播*/
$(function() {
	var oBotton = $("#button");
	var aLi = $("#button li");
	var oIng = $("#ing");
	var aImg = $("#ing img");
	 var oNext=$("#next");
	var Now = 0;
	$(document).ready(function() {
		aLi[0].className = "select";
		aImg[0].className = "select";
	});
	for (var i = 0; i < aLi.length; i++) {
		aLi[i].index = i;

		aLi[i].onmouseover = function() {
			Now = this.index;
			changeImg(this.index);
		};
		 oNext.onclick = function() {
             Now++;
         Now=Now%aLi.length;
        changeImg(Now);
     };
		function changeImg(idx) {
			for (i = 0; i < aLi.length; i++) {
				aLi[i].className = "";
				aImg[i].className = "";
			}
			aLi[idx].className = "select";
			aImg[idx].className = "select";
		};
		
	};
	
	var time;
	function run() {
		time = setInterval(function() {
			oNext.onclick();
		}, 2000);
	}
	run();
	
});
//司机风采图片移动
window.onload = function() {
	var LeftMyMar;
	var speed = 30;
	var MyMar;
	function MarqueeLeft() {
		clearInterval(MyMar);
		demo2.innerHTML = demo1.innerHTML;
		// 向左走
		function Marquee() {
			if (demo2.offsetWidth - image.scrollLeft <= 0) {
				image.scrollLeft -= demo1.offsetWidth;
			} else {
				image.scrollLeft++;
			}
		}
		LeftMyMar = setInterval(Marquee, speed);
		image.onmouseover = function() {
			clearInterval(LeftMyMar);
		}
		image.onmouseout = function() {
			LeftMyMar = setInterval(Marquee, speed);
		}
	}
	MarqueeLeft();
};

/* 选项卡点击事件 */
$(function() {
	$(".card").on('click', '.item', function() {
		var target = $(this);
		if (!target.hasClass('selected')) {
			var car = this.dataset.car;
			var content = $(".text .content[data-text=" + car + "]");
			var seletedContent = content.siblings('.selected');
			xxc(target, content, seletedContent);
		}
	});
	$(".js").on('click', '.itemd', function() {
		var target = $(this);
		if (!target.hasClass('selected')) {
			var jie = this.dataset.jie;
			var content = $(".tent .contented[data-tent=" + jie + "]");
			var seletedContent = content.siblings('.selected');
			xxc(target, content, seletedContent);
		}
	});
	$(".tab").on('click', '.item', function() {
		var target = $(this);
		if (!target.hasClass('selected')) {
			var hot = this.dataset.hot;
			var content = $(".image .content[data-content=" + hot + "]");
			var seletedContent = content.siblings('.selected');
			xxc(target, content, seletedContent);
		}
	});
	// 选项卡共用方法
	function xxc(target, content, seletedContent) {
		seletedContent.fadeOut(function() {
			target.addClass('selected').siblings('.selected').toggleClass(
					'selected');
			seletedContent.toggleClass('selected');
			content.fadeIn(function() {
				content.toggleClass('selected');
			});
		});
	}
});
//点击车辆图片，跳转到车辆订单界面
$(function() {
	$(".tu").on("click",function(){
		var target=$(this);
		var cid=target.attr("cid");
		var quDay=$("input[name=zquDay]").val();
		var quTime=$("select[name=zquTime] option:selected").text();
		var quLocation=$("select[name=zqulocation] option:selected").attr("data-id");
		var quStore=$("select[name=zqustore] option:selected").attr("data-id");
		var huanDay=$("input[name=zhuanDay]").val();
		var huanTime=$("select[name=zhuanTime] option:selected").text();
		var huanLocation=$("select[name=zhuanlocation] option:selected").attr("data-id");
		var huanStore=$("select[name=zhuanstore] option:selected").attr("data-id");
		window.location.href="goods/order?id="+cid+"&quday="+quDay+"&qutime="+quTime+"&qulocation="+quLocation+"&qustore="+quStore+"&huanday="+huanDay+"&huantime="+huanTime+"&huanlocation="+huanLocation+"&huanstore="+huanStore;
	});
});
//日历
$(function(){
	var date=new Date();
	var dateTime=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
	if(date.getHours()>=20){//判断当前时间
		$(".qutime .day").val(calTime(dateTime,1));
		$(".jietime .day").val(calTime(dateTime,1));
		$(".songtime .day").val(calTime(dateTime,1));
		$(".huantime .day").val(calTime($(".qutime .day").val(),2));
	}else{
		$(".qutime .day").val(dateTime);
		$(".jietime .day").val(dateTime);
		$(".songtime .day").val(dateTime);
		$(".huantime .day").val(calTime($(".qutime .day").val(),2));
	}
	
	/**
	 * 取车时间规定
	 */
	$("select[name=zquTime],select[name=bquTime],select[name=jieTime],select[name=songTime]").change(function(){
		var time=$(this).has("option:selected").val().split(":");
		var hour=time[0];
		var minute=time[1];
		var curhour=date.getHours();
		if(curhour>=20){
			layer.alert("夜太深，请预定明天吧");
			$(".qutime .day").val(calTime(dateTime,1));
			$(".jietime .day").val(calTime(dateTime,1));
			$(".songtime .day").val(calTime(dateTime,1));
			return;
		}
		if(curhour<=9){
			curhour=7;
		}
		if(hour<curhour+2){
			layer.alert("请选择两小时后的时间！");
			$(this).has("option").each(function(){
				var qutime=appendzero(curhour+2)+":"+minute;
				if($(this).val()==qutime){
					$(this).prop("selected","selected");
					$(this).siblings().removeProp("selected");
				};
			});
		}
		
	});
	/**
	 * 时间初始化
	 */
	$("select[name=zquTime] option,select[name=bquTime] option,select[name=jieTime] option,select[name=songTime] option").each(function(){
		var curtime=appendzero(date.getHours()+2)+":00";
		if($(this).val()==curtime){
			$(this).attr("selected","selected");
		};
	});
	/**
	 * 如果取车日期为空则不允许选择还车日期
	 */
	if($(".qutime .day").val()==""){
		$(".huantime .day").attr("disabled","disabled");
		$(".huantime .day").val("");
	}
	/**
	 * 计算时间
	 * @returns
	 */
	function calTime(value,count){
		var date=new Date(Date.parse(value.replace(/-/g,"/")));
		date.setDate(date.getDate()+count);
		var year = date.getFullYear();
		var month = (date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1);
		var day = date.getDate()<10?"0"+date.getDate():date.getDate();
		return year+"-"+month+"-"+day;
	}
	/**
	 * 补0
	 * @param obj
	 * @returns
	 */
	function appendzero (obj) {
		 if (obj < 10) return "0" + obj; else return obj;
	}
	/**
	 * 门店地点动态改变
	 */
	$(".location").change(function(){
		var target=$(this);
		var dataId=target.find("option:selected").attr("data-id");
		$.get("updateaddress",{pid:dataId},function(data){
			target.siblings(".store").html("");
			$.each($.parseJSON(data), function(i, item) {
				var option=$("<option value="+item.psName+" data-id="+item.psId+">").text(item.psName);
				target.siblings(".store").append(option);
			});
		});
	});
	
});
/**
 * 去选车按钮跳转页面
 * @returns
 */
function gotoPick(quDay,quTime,huanDay,huanTime,qulocation,qustore,huanlocation,huanstore){
	var quDay=$("input[name="+quDay+"]").val();
	var quTime=$("select[name="+quTime+"] option:selected").text();
	var quLocation=$("select[name="+qulocation+"] option:selected").attr("data-id");
	var quStore=$("select[name="+qustore+"] option:selected").attr("data-id");
	var huanDay=$("input[name="+huanDay+"]").val();
	var huanTime=$("select[name="+huanTime+"] option:selected").text();
	var huanLocation=$("select[name="+huanlocation+"] option:selected").attr("data-id");
	var huanStore=$("select[name="+huanstore+"] option:selected").attr("data-id");
	//判断时间
	var qu=quDay+" "+$.trim(quTime)+":00";
	var huan=huanDay+" "+$.trim(huanTime)+":00";
	if(compareCurrent(qu) && compareDate(qu,huan)){
		if(quDay=="zquDay"){
			window.location.href="goods?quday="+quDay+"&type=0&qutime="+quTime+"&qulocation="+quLocation+"&qustore="+quStore+"&huanday="+huanDay+"&huantime="+huanTime+"&huanlocation="+huanLocation+"&huanstore="+huanStore;
		}else{
			window.location.href="goods?quday="+quDay+"&type=1&qutime="+quTime+"&qulocation="+quLocation+"&qustore="+quStore+"&huanday="+huanDay+"&huantime="+huanTime+"&huanlocation="+huanLocation+"&huanstore="+huanStore;
		}
	}else{
		layer.alert("请选择有效时间！");
	}
}
function gotoAir(jieDay,jieTime,jielocation,jiestore){
	var jieDay=$("input[name="+jieDay+"]").val();
	var jieTime=$("select[name="+jieTime+"] option:selected").text();
	var jielocation=$("select[name="+jielocation+"] option:selected").attr("data-id");
	var jiestore=$("select[name="+jiestore+"] option:selected").attr("data-id");
	//判断时间
	var qu=jieDay+" "+$.trim(jieTime)+":00";
	if(compareCurrent(qu)){
		window.location.href="goods?quday="+jieDay+"&type=2&qutime="+jieTime+"&qulocation="+jielocation+"&qustore="+jiestore;
	}else{
		layer.alert("请选择有效时间！");
	}
}

/**
 * 判断日期在今天之前还是之后,返回是否在当前时间之后
 */
function compareCurrent(d){
	var date=new Date();
	var time=new Date(d.replace(/\-/g,'/'));
	return time>date;
}
/**
 * 判断两个日期先后，返回前是否在后之前
 * @param d1
 * @param d2
 * @returns
 */
function compareDate(d1,d2){
	var time1=new Date(d1.replace(/\-/g,'/'));
	var time2=new Date(d2.replace(/\-/g,'/'));
	return time1<time2;
}